package com.yunhe.energy.controller.data;

import com.yunhe.common.constant.EnergyConsts;
import com.yunhe.energy.domain.data.ElectricityDay;
import com.yunhe.energy.domain.data.ElectricityDetail;
import com.yunhe.energy.domain.data.ElectricityMonth;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.energy.model.CompanySort;
import com.yunhe.common.model.DomainList;
import com.yunhe.common.model.PageParam;
import com.yunhe.common.model.ResultObject;
import com.yunhe.energy.service.data.ElectricityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 电量Controller
 * @author liuronglei
 */
@Api(value = "electricity", description = "电量维护", tags = {"Data_Electricity"})
@RestController
@RequestMapping("/electricitys")
public class ElectricityController {
    @Autowired
    private ElectricityService electricityService;

    /**
     * 新增电量（日）
     * @param electricity 电量对象
     */
    @ApiOperation("新增电量（日）")
    @RequestMapping(value = "/day", method = RequestMethod.POST)
    public ResponseEntity<ResultObject> postDay(
            @RequestBody ElectricityDay electricity)
            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
        ElectricityDay newElectricity = electricityService.createElectricityDay(electricity);
        return new ResponseEntity<>(new ResultObject<>(newElectricity), HttpStatus.OK);
    }

    /**
     * 新增电量（月）
     * @param electricity 电量对象
     */
    @ApiOperation("新增电量（月）")
    @RequestMapping(value = "/month", method = RequestMethod.POST)
    public ResponseEntity<ResultObject> postMonth(
            @RequestBody ElectricityMonth electricity)
            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
        ElectricityMonth newElectricity = electricityService.createElectricityMonth(electricity);
        return new ResponseEntity<>(new ResultObject<>(newElectricity), HttpStatus.OK);
    }

    /**
     * 批量新增电量（日）
     * @param domainList 电量对象列表
     */
    @ApiOperation("批量新增电量（日）")
    @RequestMapping(value = "/day/batch", method = RequestMethod.POST)
    public ResponseEntity<ResultObject> postDay(
            @RequestBody DomainList<ElectricityDay> domainList)
            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
        List<ElectricityDay> electricityList = domainList.getList();
        Iterable<ElectricityDay> newElectricitys = electricityService.createElectricityDay(electricityList);
        return new ResponseEntity<>(new ResultObject<>(newElectricitys), HttpStatus.OK);
    }

    /**
     * 批量新增电量（月）
     * @param domainList 电量对象列表
     */
    @ApiOperation("批量新增电量（月）")
    @RequestMapping(value = "/month/batch", method = RequestMethod.POST)
    public ResponseEntity<ResultObject> postMonth(
            @RequestBody DomainList<ElectricityMonth> domainList)
            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
        List<ElectricityMonth> electricityList = domainList.getList();
        Iterable<ElectricityMonth> newElectricitys = electricityService.createElectricityMonth(electricityList);
        return new ResponseEntity<>(new ResultObject<>(newElectricitys), HttpStatus.OK);
    }

    /**
     * 根据条件获得电量列表（月）
     * @param deviceId 设备ID
     * @param dtime 时间范围
     * @param pageParam 分页对象
     */
    @ApiOperation("根据条件获得电量列表（月）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "dtime", value = "时间范围，如“2017-12-06 00:00:00,2017-12-06 23:45:00”", dataType = "date", paramType = "query")
    })
    @RequestMapping(value = "/month", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchMonth(
            @RequestParam("deviceId") Long deviceId,
            @RequestParam("dtime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Optional<List<Date>> dtime,
            @ModelAttribute PageParam pageParam)
            throws ObjectNotFoundException {
        Iterable<ElectricityMonth> results = electricityService.findElectricityMonth(deviceId, dtime.orElse(null), pageParam);
        if (results != null && results instanceof Page) {
            Page pageObj = (Page) results;
            return new ResponseEntity<>(new ResultObject<>(pageObj.getContent(), pageObj.getNumber(), pageObj.getSize(), pageObj.getTotalElements(), pageObj.getTotalPages()), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
        }
    }

    /**
     * 根据条件获得电量列表（明细）
     * @param deviceId 对象ID
     * @param dtime 时间范围
     * @param pageParam 分页对象
     */
    @ApiOperation("根据条件获得电量列表（明细）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "对象ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "dtime", value = "时间范围，如“2017-12-06 00:00:00,2017-12-06 23:45:00”", dataType = "Date", paramType = "query")
    })
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchDetail(
            @RequestParam("deviceId") Long deviceId,
            @RequestParam("dtime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Optional<List<Date>> dtime,
            @ModelAttribute PageParam pageParam)
            throws ObjectNotFoundException {
        Iterable<ElectricityDetail> results = electricityService.findElectricityDetail(deviceId, dtime.orElse(null), pageParam);
        if (results != null && results instanceof Page) {
            Page pageObj = (Page) results;
            return new ResponseEntity<>(new ResultObject<>(pageObj.getContent(), pageObj.getNumber(), pageObj.getSize(), pageObj.getTotalElements(), pageObj.getTotalPages()), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
        }
    }

    /**
     * 根据条件获得电量列表（日）
     * @param deviceId 对象ID
     * @param dtime 时间范围
     * @param pageParam 分页对象
     */
    @ApiOperation("根据条件获得电量列表（日）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "对象ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "dtime", value = "时间范围，如“2017-12-06 00:00:00,2017-12-06 23:45:00”", dataType = "date", paramType = "query")
    })
    @RequestMapping(value = "/day", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchDay(
            @RequestParam("deviceId") Optional<Long> deviceId,
            @RequestParam("dtime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Optional<List<Date>> dtime,
            @ModelAttribute PageParam pageParam)
            throws ObjectNotFoundException {
        Iterable<ElectricityDay> results = electricityService.findElectricityDay(deviceId.orElse(null), dtime.orElse(null), pageParam);
        if (results != null && results instanceof Page) {
            Page pageObj = (Page) results;
            return new ResponseEntity<>(new ResultObject<>(pageObj.getContent(), pageObj.getNumber(), pageObj.getSize(), pageObj.getTotalElements(), pageObj.getTotalPages()), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
        }
    }

    /**
     * 获得累计电量
     * @param deviceId 设备ID
     * @param dtime 日期
     * @param type 电量类型
     */
    @ApiOperation("获得累计电量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "dtime", value = "日期", required = true, dataType = "date", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "电量类型", dataType = "date", paramType = "query")
    })
    @RequestMapping(value = "/amount", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> getAmount(
            @RequestParam("deviceId") Long deviceId,
            @RequestParam("realtime") Optional<Boolean> realtime,
            @RequestParam("dtime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") List<Date> dtime,
            @RequestParam("type") Optional<EnergyConsts.ELECTRICITY_TYPE> type)
            throws ObjectNotFoundException {
        EnergyConsts.ELECTRICITY_TYPE myType = type.orElse(null);
        Double result = electricityService.getDayCount(deviceId, dtime, myType);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    /**
     * 获得企业能耗排名
     * @param firmId 单位ID
     * @param stationId 电站ID
     * @param dtime 日期
     */
    @ApiOperation("获得企业能耗排名")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "firmId", value = "单位ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "stationId", value = "电站ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "dtime", value = "日期", required = true, dataType = "date", paramType = "query")
    })
    @RequestMapping(value = "/companySort", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> getCompanySort(
            @RequestParam("firmId") Optional<Long> firmId,
            @RequestParam("stationId") Optional<Long> stationId,
            @RequestParam("dtime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date dtime,
            @ModelAttribute PageParam pageParam) {
        List<CompanySort> result = electricityService.getCompanyMonthSort(firmId.orElse(null), stationId.orElse(null), dtime, pageParam);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }
}